package controller

import (
	"common/helper"
	"common/util"
	"encoding/json"
	"errors"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"member/model"
)

type SystemDataSettingController struct {
	util.ControllerImpl
}

var systemDataSettingModel = &model.SystemDataSettingModel{
	TableName: "tbl_system_data_setting",
}

func (that *SystemDataSettingController) GetSystemDataSettingByDataKey(ctx *fasthttp.RequestCtx) {
	err := func(ctx *fasthttp.RequestCtx) error {
		dataKey := string(ctx.QueryArgs().Peek("dk"))
		if dataKey == "" {
			return errors.New(helper.ParamErr)
		}
		that.GetByEx(ctx, g.Ex{"data_key": dataKey})
		return nil
	}(ctx)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
}

func (that *SystemDataSettingController) GetModel() *model.SystemDataSettingModel {
	return systemDataSettingModel
}
func (that *SystemDataSettingController) Insert(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.SystemDataSettingParams{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New(helper.ParamErr)
		}
		err := that.GetModel().Insert(ent)
		if err != nil {
			return err
		}
		helper.Print(ctx, true, helper.Success)
		return nil
	})
}
func (that *SystemDataSettingController) Update(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.SystemDataSetting{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New(helper.ParamErr)
		}
		err := that.GetModel().Update(ent)
		if err != nil {
			return err
		}
		helper.Print(ctx, true, helper.Success)
		return nil
	})
}
func (that *SystemDataSettingController) Delete(ctx *fasthttp.RequestCtx) {
	that.ExecutionGetId(ctx, func(id uint32) error {
		err := that.GetModel().DeleteById(id)
		if err != nil {
			return err
		}
		helper.Print(ctx, true, helper.Success)
		return nil
	})
}
func (that *SystemDataSettingController) Get(ctx *fasthttp.RequestCtx) {
	that.ExecutionGetId(ctx, func(id uint32) error {
		ent := model.SystemDataSetting{}
		err := that.GetModel().Get(id, &ent)
		if err != nil {
			return err
		}
		helper.Print(ctx, true, ent)
		return nil
	})
}
func (that *SystemDataSettingController) GetByEx(ctx *fasthttp.RequestCtx, ex g.Expression) {
	ent := model.SystemDataSetting{}
	err := that.GetModel().GetByEx(ex, &ent)
	if err == nil {
		helper.Print(ctx, true, ent)
		return
	}
	helper.Print(ctx, false, err)
}
func (that *SystemDataSettingController) Page(ctx *fasthttp.RequestCtx) {
	that.ExecutionPage(ctx, func(page *model.SystemDataSettingPageParams) error {
		err := that.GetModel().Page(page)
		if err != nil {
			return err
		}
		helper.Print(ctx, true, page)
		return nil
	})
}
func (that *SystemDataSettingController) ExecutionPage(ctx *fasthttp.RequestCtx, f func(page *model.SystemDataSettingPageParams) error) {
	that.Execution(ctx, func(params []byte) error {
		pageParams := &model.SystemDataSettingPageParams{}
		if e := json.Unmarshal(params, &pageParams); e != nil {
			return errors.New(helper.ParamErr)
		}
		return f(pageParams)
	})
}
